
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using Tathata.Common.Utils;
using Tathata.Common.BLL.EntityCollections;
using System.Collections.Generic;
using Tathata.Web.Administration.Modules;
namespace Tathata.Web.Administration
{
    public partial class Administration_ManufacturerDetails : BaseNopAdministrationPage
    {
        private void BindData()
        {
            Manufacturer manufacturer = ManufacturerManager.GetByManufacturerID(this.ManufacturerID);
            if (manufacturer != null)
            {
                this.txtName.Text = manufacturer.Name;
                this.txtDescription.Value = manufacturer.Description;
                CommonHelper.SelectListItem(this.ddlTemplate, manufacturer.TemplateID);
                this.txtMetaKeywords.Text = manufacturer.MetaKeywords;
                this.txtMetaDescription.Text = manufacturer.MetaDescription;
                this.txtMetaTitle.Text = manufacturer.MetaTitle;
                this.txtSEName.Text = manufacturer.SEName;

                Picture manufacturerPicture = manufacturer.Picture;
                btnRemoveManufacturerImage.Visible = manufacturerPicture != null;
                string pictureUrl = PictureManager.GetPictureUrl(manufacturerPicture, 100, 80);
                this.iManufacturerPicture.ImageUrl = pictureUrl;

                this.cbPublished.Checked = manufacturer.Published;
                this.txtDisplayOrder.Value = manufacturer.DisplayOrder;

                ProductManufacturerCollection existingProductManufacturerCollection = manufacturer.ProductManufacturers;
                List<ProductManufacturerMappingHelperClass> productManufacturerMappings = GetProductManufacturerMappings(existingProductManufacturerCollection);
                gvProductMappings.DataSource = productManufacturerMappings;
                gvProductMappings.DataBind();
            }
            else
                Response.Redirect("Manufacturers.aspx");
        }

        private void FillDropDowns()
        {
            this.ddlTemplate.Items.Clear();
            ManufacturerTemplateCollection manufacturerTemplates = ManufacturerTemplateManager.GetAllManufacturerTemplates();
            foreach (ManufacturerTemplate manufacturerTemplate in manufacturerTemplates)
            {
                ListItem item2 = new ListItem(manufacturerTemplate.Name, manufacturerTemplate.ManufacturerTemplateID.ToString());
                this.ddlTemplate.Items.Add(item2);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.FillDropDowns();
                this.BindData();
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Manufacturer manufacturer = ManufacturerManager.GetByManufacturerID(this.ManufacturerID);

                    if (manufacturer != null)
                    {
                        Picture manufacturerPicture = manufacturer.Picture;
                        HttpPostedFile manufacturerPictureFile = fuManufacturerPicture.PostedFile;
                        if ((manufacturerPictureFile != null) && (!String.IsNullOrEmpty(manufacturerPictureFile.FileName)))
                        {
                            byte[] manufacturerPictureBinary = PictureManager.GetPictureBits(manufacturerPictureFile.InputStream, manufacturerPictureFile.ContentLength);
                            if (manufacturerPicture != null)
                                manufacturerPicture = PictureManager.UpdatePicture(manufacturerPicture.PictureID, manufacturerPictureBinary, manufacturerPictureFile.ContentType, true);
                            else
                                manufacturerPicture = PictureManager.InsertPicture(manufacturerPictureBinary, manufacturerPictureFile.ContentType, true);
                        }
                        int manufacturerPictureID = 0;
                        if (manufacturerPicture != null)
                            manufacturerPictureID = manufacturerPicture.PictureID;

                        manufacturer = ManufacturerManager.UpdateManufacturer(this.ManufacturerID, txtName.Text, txtDescription.Value, int.Parse(this.ddlTemplate.SelectedItem.Value),
                             txtMetaKeywords.Text, txtMetaDescription.Text, txtMetaTitle.Text, txtSEName.Text,
                            manufacturerPictureID, cbPublished.Checked, manufacturer.Deleted, txtDisplayOrder.Value, manufacturer.CreatedOn, DateTime.Now);

                        foreach (GridViewRow row in gvProductMappings.Rows)
                        {
                            CheckBox cbProductInfo = row.FindControl("cbProductInfo") as CheckBox;
                            HiddenField hfProductID = row.FindControl("hfProductID") as HiddenField;
                            HiddenField hfProductManufacturerID = row.FindControl("hfProductManufacturerID") as HiddenField;
                            CheckBox cbFeatured = row.FindControl("cbFeatured") as CheckBox;
                            NumericTextBox txtRowDisplayOrder = row.FindControl("txtDisplayOrder") as NumericTextBox;
                            int productManufacturerID = int.Parse(hfProductManufacturerID.Value);
                            int productID = int.Parse(hfProductID.Value);
                            int displayOrder = txtRowDisplayOrder.Value;

                            if (productManufacturerID > 0 && !cbProductInfo.Checked)
                                ProductManufacturerManager.DeleteProductManufacturer(productManufacturerID);
                            if (productManufacturerID > 0 && cbProductInfo.Checked)
                                ProductManufacturerManager.UpdateProductManufacturer(productManufacturerID, productID, manufacturer.ManufacturerID, cbFeatured.Checked, displayOrder);
                            if (productManufacturerID == 0 && cbProductInfo.Checked)
                                ProductManufacturerManager.InsertProductManufacturer(productID, manufacturer.ManufacturerID, cbFeatured.Checked, displayOrder);
                        }
                    }

                    Response.Redirect(string.Format("ManufacturerDetails.aspx?ManufacturerID={0}", manufacturer.ManufacturerID));
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                ManufacturerManager.MarkManufacturerAsDeleted(this.ManufacturerID);
                Response.Redirect("Manufacturers.aspx");
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }

        protected void btnRemoveManufacturerImage_Click(object sender, EventArgs e)
        {
            try
            {
                Manufacturer manufacturer = ManufacturerManager.GetByManufacturerID(this.ManufacturerID);
                if (manufacturer != null)
                {
                    PictureManager.DeletePicture(manufacturer.PictureID);
                    ManufacturerManager.RemoveManufacturerPicture(manufacturer.ManufacturerID);
                    BindData();
                }
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }

        private List<ProductManufacturerMappingHelperClass> GetProductManufacturerMappings(ProductManufacturerCollection ExistingProductManufacturerCollection)
        {
            ProductCollection productCollection = ProductManager.GetAllProducts();
            List<ProductManufacturerMappingHelperClass> result = new List<ProductManufacturerMappingHelperClass>();
            for (int i = 0; i < productCollection.Count; i++)
            {
                Product product = productCollection[i];
                ProductManufacturer existingProductManufacturer = ExistingProductManufacturerCollection.FindProductManufacturer(product.ProductID, this.ManufacturerID);
                ProductManufacturerMappingHelperClass pcm = new ProductManufacturerMappingHelperClass();
                if (existingProductManufacturer != null)
                {
                    pcm.ProductManufacturerID = existingProductManufacturer.ProductManufacturerID;
                    pcm.IsMapped = true;
                    pcm.IsFeatured = existingProductManufacturer.IsFeaturedProduct;
                    pcm.DisplayOrder = existingProductManufacturer.DisplayOrder;
                }
                else
                {
                    pcm.DisplayOrder = 1;
                }
                pcm.ProductID = product.ProductID;
                pcm.ProductInfo = product.Name;

                result.Add(pcm);
            }

            return result;
        }

        private class ProductManufacturerMappingHelperClass
        {
            public int ProductManufacturerID { get; set; }
            public int ProductID { get; set; }
            public string ProductInfo { get; set; }
            public bool IsMapped { get; set; }
            public bool IsFeatured { get; set; }
            public int DisplayOrder { get; set; }
        }

        public int ManufacturerID
        {
            get
            {
                return CommonHelper.QueryStringInt("ManufacturerID");
            }
        }
    }
}